﻿using System;
using SilverlightProgressFeedback.ServiceReference1;
using SilverlightProgressFeedback.Views;

namespace SilverlightProgressFeedback
{
    using System.Windows.Controls;
    using System.Windows.Navigation;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        private readonly Guid _slowProcessProgressFeedbackKey = Guid.NewGuid();
        private bool _slowProcessDone = false;

        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void SlowProcessWithBusyIndicatorClick(object sender, System.Windows.RoutedEventArgs e)
        {
            BusyIndicator.IsBusy = true;

            BusyIndicator.BusyContent = "Starting...";
            Service1Client client = new Service1Client();
            client.SlowProcessCompleted +=
                (object sndr, SlowProcessCompletedEventArgs args) =>
                {
                    BusyIndicator.IsBusy = false;
                    _slowProcessDone = true;
                    label.Content = args.Result;
                };


            client.SlowProcessAsync(_slowProcessProgressFeedbackKey, 10);
            RequestFeedback(_slowProcessProgressFeedbackKey);
        }

        private void RequestFeedback(Guid progressFeedbackKey)
        {
            if (_slowProcessDone == false)
            {
                Service1Client client = new Service1Client();

                client.GetProcessInfoCompleted +=
                    (a, b) =>
                    {
                        if (_slowProcessDone == false)
                        {
                            BusyIndicator.BusyContent = b.Result.Description;
                            RequestFeedback(progressFeedbackKey);
                        }
                    };

                client.GetProcessInfoAsync(progressFeedbackKey, 500);
            }
        }

        private void SlowProcessWithWindowClick(
            object sender,
            System.Windows.RoutedEventArgs e)
        {
            ProgressFeedbackWindow feedbackWindow =
                new ProgressFeedbackWindow(
                    "Running SlowProcess through WCF - with ProgressFeedback!!");

            Service1Client client = new Service1Client();
            client.InnerChannel.OperationTimeout = new TimeSpan(0, 1, 0, 0);
            client.SlowProcessCompleted +=
                (object sndr, SlowProcessCompletedEventArgs args) =>
                {
                    feedbackWindow.ProcessDone();
                    if (feedbackWindow.Aborted == false)
                    {
                        label.Content = args.Result;
                    }
                    else
                    {
                        label.Content = "USER ABORTED!";
                    }
                };

            client.SlowProcessAsync(feedbackWindow.ProgressFeedbackKey, 10);
            feedbackWindow.Show();
        }
    }
}